<?php
namespace app\first\controller;

use app\first\model\ActiveOrder;
use app\first\model\Keywords;
use think\db\Where;

/**
 * Class Article
 * @title 梦想加相关
 * @url /v1/active
 * @desc  获取活动相关
 * @version 1.0
 * @package app\first\dream
 * @route('v1/active')
 */
class Active extends Base{
    //是否开启授权认证,默认开启
    public $apiAuth = false;
    protected $model = null;
    protected $validate = null;
    /*
    * 需要附加的其他方法,比如:topList,newList
    * protected $extraActionList = ['topList','newList',...];
    * 已经对核心的类进行了重写,扩展的新方法会自动添加到认证中,无需再手动书写
    */
    protected $extraActionList = [];

    protected $skipAuthActionList= [];

    public function __construct(){
        parent::__construct();
        $this->model = new \app\first\model\Active();
        $this->validate = new \app\first\validate\Active();
    }

    /**
     * @title 获取类目列表
     * @method index
     * @param int $is_top 是否置顶 false 0 1是,0否
     * @param int $limit 每页条数 true 8
     * @param int $page 当前页数 true 1
     * @route('v1/active/list','get')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\exception\DbException
     */
    public function index($is_top=0,$limit=8,$page = 1){
        $map = [['status','eq',1]];
        $order= $is_top ? "is_top desc,create_time desc":'create_time desc';
        $list = $this->model->getList($map,$order,$limit);
        return $this->sendSuccess($list);
    }

    /**
     * @title 获取详情
     * @method read
     * @param int $id id true
     * @route('v1/active/read','get')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function read($id=0){
        $info = $this->model->getOneById($id);
        return $this->sendSuccess($info);
    }

    /**
     * @title 获取随机相关
     * @method like
     * @param int $limit 条数 true 4
     * @route('v1/active/like','get')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function like($limit=4){
        $list = $this->model->likes($limit);
        return $this->sendSuccess($list);
    }

    /**
     * @title 获取最新发布
     * @method last_publish
     * @param int $limit 条数 false 10
     * @route('v1/active/last_publish','get')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function last_publish($limit=10){
        $list = $this->model->lastList('create_time desc',$limit);
        return $this->sendSuccess($list);
    }

    /**
     * @title 搜索
     * @method search
     * @param string $q 关键词 true
     * @param int $limit 条数 true 10
     * @param int $page 页数 true 1
     * @route('v1/active/search','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     * @throws \think\exception\DbException
     */
    public function search($q='',$limit=10,$page=1){
        if(!$q){
            return $this->sendError(0,'请输入关键词');
        }
        $where = new Where();
        $where['status']=1;
        $keywords = new Keywords();
        $keywords->addNew($q,3);
        $list = $this->model->search($q,$where,$limit);
        return $this->sendSuccess($list,'');
    }

    /**
     * @title 创建订单
     * @method create_order
     * @param int $uid 用户ID true
     * @param string $title 标题 true
     * @param string $aid 活动id true
     * @param int $totals 价格 true
     * @param int $discount 优惠
     * @param string $info 介绍
     * @param int $type 类型 false 1 1微信,2支付宝
     * @route('v1/article/create_order','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     */
    public function create_order($uid=0,$title='',$aid='',$totals=0,$discount=0,$info='',$type=1){
        $order = new ActiveOrder();
        $result = $order->addNew($uid,$title,$aid,$info,$totals,$discount,$out);
        if(!$result){
            return $this->sendError($out);
        }
        $payment = (new Payment())->signature($type,'',$out['order_id'],$totals,$title);
        return $this->sendSuccess(array_merge($payment,$out));
    }

    /**
     * @title 我的活动订单
     * @method active_order
     * @param int $uid 用户ID true
     * @param int $type 类型 false  1未支付,2支付成功,3支付失败,4已发货
     * @param int $limit 每页条数 true 8
     * @param int $page 当前页数 true 1
     * @route('v1/active/my_order','get')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\exception\DbException
     */
    public function active_order($uid=0,$type=0,$limit=10){
        $order = new ActiveOrder();
        $where[]=['uid','eq',$uid];
        if($type){
            $where[]=['status','eq',$type];
        }
        $list = $order->getList($where,'create_time desc',$limit);
        return $this->sendSuccess($list);
    }
}